package com.gcstorage.tvdev.ui

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.bumptech.glide.Glide


import com.gcstorage.tvdev.base.BaseActivity
import com.gcstorage.tvdev.base.BaseViewModel
import com.gcstorage.tvdev.bean.QRCodeInfo
import com.gcstorage.tvdev.constants.ARouterPaths
import com.gcstorage.tvdev.constants.LogCat
import com.gcstorage.tvdev.databinding.ActivityLoginTvBinding
import com.gcstorage.tvdev.model.LoginViewModel
import com.king.zxing.util.CodeUtils

import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

/**
 *@author: liangkun
 *@date: 2022/3/30
 *@des:登录页面
 */
@Route(path = ARouterPaths.TV_APP_LOGIN)
class TvLoginActivity : BaseActivity() {
    private lateinit var binding: ActivityLoginTvBinding
    private lateinit var loginViewModel: LoginViewModel
    var disposable: Disposable? = null
    var code: QRCodeInfo? = null
    val handler = Handler(Looper.getMainLooper())
    override fun getRootView(): View {
        binding = ActivityLoginTvBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {

    }

    override fun initData() {
        loginViewModel.createQrCode()
    }

    @SuppressLint("CheckResult")
    override fun observeLiveData() {
        super.observeLiveData()
        //登录状态的回调
        loginViewModel.loginState.observe(this) {
            LogUtils.d(LogCat.LOG_COMMONON + " it $it")
            if (it) {
                disposable?.dispose()
                handler.removeCallbacksAndMessages(null)
//                startActivity(Intent(mActivity, LoadingDataActivity::class.java))
                ARouter.getInstance().build(ARouterPaths.TV_APP_MAIN).navigation()
                finish()
            } else {
                closeLoadingDialog()
            }
        }
        //二维码回调
        loginViewModel.qrCode.observe(this) { qrCodeInfo ->
            if (qrCodeInfo != null) {
                code = qrCodeInfo
                Glide.with(mActivity).load(CodeUtils.createQRCode(qrCodeInfo.data, 300))
                    .into(binding.ivCodeDiv)
                binding.ivCodeDiv.visibility = View.VISIBLE
                if (disposable != null) {
                    disposable?.dispose()
                    disposable = null
                }
                disposable = Observable.interval(
                    3, TimeUnit.SECONDS
                ).compose(bindToLifecycle())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe { loginViewModel.listenerQrCode(qrCodeInfo.uid) }
            } else {
                LogUtils.d(LogCat.LOG_COMMONON + " qrCodeInfo 为null...")
//                binding.tvUnuseTip.text = "手机猎鹰扫码登录"
            }
        }


        //监听回调
        loginViewModel.qrListenerBean.observe(this) {
            if (!it.isExist) {
                LogUtils.d(LogCat.LOG_COMMONON + " 二维码失效。。。。。")
//               二维码失效。重新请求刷新
                if (disposable != null) {
                    disposable?.dispose()
                    disposable = null
                }
                //延时500ms去创造二维码
                handler.postDelayed(Runnable {
                    loginViewModel.createQrCode()
                }, 500)
                return@observe
            }
            if (it.isSuccess) {
                code?.uid?.let { it1 -> loginViewModel.qrLogin(it1, it.userId) }
            }
        }


    }

    override fun createViewModel(): BaseViewModel {
        loginViewModel = getViewModelProvider().get(LoginViewModel::class.java)
        return loginViewModel

    }

    override fun onDestroy() {
        super.onDestroy()

    }
}